package com.bluishoul.action;

import java.io.IOException;

import javax.servlet.ServletException;

import my.mvc.Annotation.JSONOutputEnabled;
import my.mvc.Annotation.PostMethod;
import my.mvc.Annotation.UserRoleRequired;
import my.mvc.RequestContext;

import org.apache.commons.lang.StringUtils;

import com.bluishoul.bean.Location;
import com.bluishoul.bean.User;

public class UserAction {
	
	@PostMethod
	@JSONOutputEnabled
	public void login3rd(RequestContext rc) throws IOException, ServletException{
		User user = rc.form(User.class);
		User loginUser = user.login3rd();
		if(loginUser==null){
			user.register3rd();
			rc.saveUserInCookie(user, "1".equals(rc.param("save_login")));
			rc.output_json("id", user.getId());
		}else{
			rc.request().setAttribute(User.G_USER, user);
			rc.forward("/");
		}
	}
	
	@PostMethod
	@JSONOutputEnabled
	public void login(RequestContext rc) throws IOException{
		User user = rc.form(User.class);
		User loginUser =user.login();
		if(loginUser!=null){
			rc.saveUserInCookie(loginUser, "1".equals(rc.param("save_login")));
			rc.output_json("id", loginUser.getId());
		}else{
			throw rc.error("username_or_pwd_error");
		}
	}

	@PostMethod
	@JSONOutputEnabled
	public void register(RequestContext rc) throws ServletException, IOException{
		User user = rc.form(User.class);
		if(StringUtils.isBlank(user.getUsername()) || StringUtils.isBlank(user.getPwd()) || StringUtils.isBlank(user.getEmail())){
			throw rc.error("username_or_pwd_or_email_null");
		}
		User ex = user.hasSame();
		if(ex!=null)
			throw rc.error("user_exist");
		if(user.register()>0){
			rc.saveUserInCookie(user, false);
			rc.output_json("id", user.getId());
		}else
			throw rc.error("opt_error");
	}
	
	@PostMethod
	@JSONOutputEnabled
	@UserRoleRequired
	public void updateUserInfo(RequestContext rc) throws IOException{
		User user = rc.form(User.class);
		User u2 = (User) rc.user();
		User u = user.Get(user.getId());
		if(u==null)
			throw rc.error("user_not_exist");
		if(!u2.CanAdmin(user)){
			rc.forbidden();
			return;
		}
		if(StringUtils.isBlank(user.getUsername()) || StringUtils.isBlank(user.getPwd()) || StringUtils.isBlank(user.getEmail())){
			throw rc.error("username_or_pwd_or_email_null");
		}
		u.setUsername(user.getUsername());
		u.setPwd(user.getPwd());
		u.setEmail(user.getEmail());
		if(!u.updateUserInfo()){
			throw rc.error("user_info_update_failed");
		}
	}
	
	@UserRoleRequired
	public void logout(RequestContext rc) throws ServletException, IOException{
		User user = (User) rc.user();
		if(user!=null){
			rc.deleteUserInCookie();
			rc.deleteLocInCookie();
		}
		rc.redirect("/");
	}
	
	@PostMethod
	public void locate(RequestContext rc){
		Location loc = rc.form(Location.class);
		if(StringUtils.isNotBlank(loc.getProvince()) && StringUtils.isNotBlank(loc.getCity()) && StringUtils.isNotBlank(loc.getDistrict()))
			rc.saveLocationInCookie(loc, false);
	}	
}
